#include "SPI_HAL_QG.h"

void vfnSPI_PrescalerSelect (uint8 u8prescaler)
{
    SPIBR_SPPR = u8prescaler;    
}

void vfnSPI_BaudRateSelect (uint8 u8baudrate)
{
    SPIBR_SPR = u8baudrate;   
}

void vfnSPI_SetSystemStatus (uint8 u8status)
{
    SPIC1_SPE = u8status;    
}

void vfnSPI_ModeSelect (uint8 u8mode)
{
    SPIC1_MSTR = u8mode;
}

uint8 u8SPI_RxPending (void)
{
    return SPIS_SPRF;
}

uint8 u8SPI_TxComplete (void)
{
    return SPIS_SPTEF;
}

uint8 u8SPI_ReadData (void)
{
    (void)SPIS_SPTEF;
    SPID = 0xFF;
    while(!SPIS_SPRF) { }
    return SPID;
}

void vfnSPI_SendByte (uint8 u8byte)
{
    (void)SPIS_SPTEF;
    SPID = u8byte;
    while(!SPIS_SPTEF) { }
}

void vfnSPI_Delay(uint8 u8delay)
{
    while(u8delay--)
    {    
        vfnSPI_SendByte (0xFF);    
    }
}

void vfnSPI_Init (void)
{
    vfnSPI_ModeSelect (SPI_MASTER_MODE);
    vfnSPI_SetSystemStatus (SPI_ENABLED);   
}